﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;
using Xiucai.BPM.Core;
using Xiucai.BPM.Core.Bll;
using Xiucai.BPM.Core.Dal;
using Xiucai.BPM.Core.Model;
using Xiucai.Common;
using Omu.ValueInjecter;
using Xiucai.Common.Data.SqlServer;
using System.Data.SqlClient;
using System.Data;
using Xiucai.Common.Data;
namespace Xiucai.BPM.Admin.Dev.ashx
{
    /// <summary>
    /// DataTestHandler 的摘要说明
    /// </summary>
    public class DevConfigHandler : IHttpHandler, IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            
            if(SysVisitor.Instance.IsGuest)
            {
                context.Response.Write(
                    new JsonMessage { Success = false, Data = "-99", Message = "登录已过期，请重新登录" }.ToString()
                    );
                context.Response.End();
            }

            var json = HttpContext.Current.Request["json"];
            var rpm = new RequestParamModel<Xiucai.BPM.Core.Model.DevConfig>(context) { CurrentContext = context, Action = context.Request["action"] };
            if (!string.IsNullOrEmpty(json))
            {
                rpm = JSONhelper.ConvertToObject<RequestParamModel<Xiucai.BPM.Core.Model.DevConfig>>(json);
                rpm.CurrentContext = context;
            }

            switch (rpm.Action)
            {
                case "names": //读取字典类别
                    if (context.Request["searcht"] != null && context.Request["searcht"].ToString()!="")
                    context.Response.Write(UserBll.Instance.UserTwoNames(context.Request["searcht"]));
                    else
                      context.Response.Write(UserBll.Instance.UserTwoNames());  
                    break;

                case "add":
                    var dc = new Xiucai.BPM.Core.Model.DevConfig
                    {
                        DevID = PublicMethod.GetInt(rpm.Request("DevID")),
                        KeyId = PublicMethod.GetInt(rpm.Request("KeyId")),
                        DevType = rpm.Request("DevType"),
                        DevOwnerName = rpm.Request("DevOwnerName"),
                        DevOwnerID = PublicMethod.GetInt(rpm.Request("DevOwnerID")),
                        DevContact = rpm.Request("DevContact"),
                        DevLocation = rpm.Request("DevLocation"),
                        chn0 = rpm.Request("chn0"),
                        chn1 = rpm.Request("chn1"),
                        chn2 = rpm.Request("chn2"),
                        chn3 = rpm.Request("chn3"),
                        XQHID = rpm.Request("XQHID"),
                        //mjd = rpm.Request("mjd"),
                        mmark = rpm.Request("mmark")
                    };
                    AddDevConfig(dc, context);
                    break;
                case "edit":
                    Xiucai.BPM.Core.Model.DevConfig u = new Xiucai.BPM.Core.Model.DevConfig();
                    u.InjectFrom(rpm.Entity);
                    u.KeyId = rpm.KeyId;                    
                    context.Response.Write(DevConfigBll.Instance.Edit(u));                    
                    break;

                case "del":
                    var dconfig = PublicMethod.GetInt(rpm.KeyId);
                    string  devtype= PublicMethod.GetString(rpm.Request("devtype"));
                    int ddevid = PublicMethod.GetInt(rpm.Request("devid"));
                    string dmsg = DevConfigBll.Instance.Delete(dconfig,devtype,ddevid);
                    context.Response.Write(dmsg);   
                    break;
                case "getmy":
                    var userid = PublicMethod.GetInt(rpm.Request("uid"));
                    string devJ = DevConfigBll.Instance.GetDevListByUid(userid);
                    context.Response.Write(devJ);
                    break;
                case "getmylist":
                    var xqhid = PublicMethod.GetString(rpm.Request("XQHID"));
                    string devp = DevConfigBll.Instance.GetDevListByPhone(xqhid);
                    context.Response.Write(devp);
                    break;
                case "getmydevxqh":
                    var id = PublicMethod.GetInt(rpm.Request("keyid"));
                    string devxqh = DevConfigBll.Instance.GetDevListByKeyid(id);
                    context.Response.Write(devxqh);
                    break;
                case "setdev":
                    var keyid = PublicMethod.GetInt(rpm.Request("KeyId"));
                    var devid = PublicMethod.GetString(rpm.Request("DevType"));                 
                    context.Response.Write(DevConfigBll.Instance.Edit(keyid,devid));                    
                   
                    break;
                default: //字典列表, or 设备列表
                    var userID = PublicMethod.GetInt(rpm.Request("categoryId"));
                    int mobile = PublicMethod.GetInt(rpm.Request("mobile"));
                    //string dicJson = DicBll.Instance.GetDicListBy(userID);
                    string devJson = DevConfigBll.Instance.GetDevListBy(userID, mobile);
                    context.Response.Write(devJson);
                    break;
            }


        }

        void AddDevConfig(Xiucai.BPM.Core.Model.DevConfig dc, HttpContext context)
        {
            int k = DevConfigDal.Instance.Insert(dc);
            var msg = "添加成功。";
            if (k < 0)
                msg = "添加失败。";
            context.Response.Write(new JsonMessage { Success = true, Data = k.ToString(), Message = msg }.ToString());
        }

        void EditCategory(DicCategory dc,HttpContext context)
        {
            int k = DicCategoryDal.Instance.Update(dc);
            var msg = "编辑成功。";
            if (k <= 0)
                msg = "编辑失败。";
            context.Response.Write(new JsonMessage { Success = true, Data = k.ToString(), Message = msg }.ToString());
        }

        void DelDevConfig(int cateId, HttpContext context)
        {
            int k=0;
            using (var conn = new SqlConnection(SqlEasy.connString))
            using (var cmd = conn.CreateCommand())
            {
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = "delete from Dev_Dev where DevID=@KeyID";

                cmd.InjectFrom<SetParamsValues>(new { KeyID = cateId });
                conn.Open();
                k= cmd.ExecuteNonQuery();
            }
            var msg = "删除成功。";
            if (k <= 0)
                msg = "删除失败。";
            context.Response.Write(new JsonMessage { Success = true, Data = k.ToString(), Message = msg }.ToString());
        }


        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }


}